<template>
  <a-modal
    title="新增活动"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template #footer>
      <a-button key="back" type="primary" :loading="loading" @click="handle">仅保存</a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk" style="background-color: #D15B47!important;border-color: #D15B47!important">保存并开启</a-button>
    </template>
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="id">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入名称' }] }]" placeholder="请输入名称" />
        </a-form-item>
        <a-form-item label="赠送积分">
          <a-input v-decorator="['sendIntegral', { rules: [{ required: true, message: '请输入赠送积分' }] }]" placeholder="请输入赠送积分" />
        </a-form-item>
        <a-form-item label="持续天数">
          <a-input v-decorator="['days', { rules: [{ required: true, message: '请输入持续天数' }] }]" placeholder="请输入持续天数" />
        </a-form-item>
        <a-form-item label="循环活动">
          <a-checkbox v-decorator="['isRepet']">循环活动，结束后自动开始下一次</a-checkbox>
        </a-form-item>
        <a-form-item label="私密活动">
          <a-checkbox v-decorator="['actType']">私密活动，不公开发布，需要一对一赠送</a-checkbox>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['name', 'isRepet', 'actType', 'days', 'sendIntegral', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handle(){
      this.$emit('ok',{ value:2 })
    },
    handleCancel() {
      this.$emit('cancel')
    },
    handleOk() {
      this.$emit('ok',{ value:1 })
    },
  },
}
</script>
